﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using BackupConsoleClient.BackupServiceReference;

namespace BackupConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0 ||
                (args.Length == 1 && args[0].Contains("help")))
            {
                //Display help file.
                Console.WriteLine(File.ReadAllText(System.Configuration.ConfigurationManager.AppSettings["readme"]));
            }
            else
            {
                Run();
            }

            Console.ReadKey();
        }

        private static void Run()
        {
            Guid fileId;
            switch (CommandLineArguments.Instance.Command)
            {
                case CommandType.Save:
                    //Save a file into Backup storage.

                    //Check if the file exist.
                    if (!File.Exists(CommandLineArguments.Instance.File))
                    {
                        Console.WriteLine(string.Format("The given file cannot be found on the computer: {0}"));
                        break;
                    }

                    BackupEntityCommandActions.Save();
                    break;
                case CommandType.LoadById:
                    //Load the file from backup system based on the id.                    

                    //Validate the file id.                    
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Name) ||
                        !Guid.TryParse(CommandLineArguments.Instance.Name, out fileId))
                    {
                        Console.WriteLine("The name of the file is not valid. The name have to be a GUID.");
                        break;
                    }

                    WriteInformation(BackupEntityCommandActions.LoadById());
                    break;

                case CommandType.LoadByName:
                    //Load the file from backup system based on the name of the file.                    

                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Name))
                    {
                        Console.WriteLine("The name of the file is not specify.");
                        break;
                    }

                    WriteInformation(BackupEntityCommandActions.LoadByName());
                    break;

                case CommandType.LoadContentToFile:
                    //Load the stream from backup system based on the id of the file.

                    //Validate the file id.
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Name) ||
                        !Guid.TryParse(CommandLineArguments.Instance.Name, out fileId))
                    {
                        Console.WriteLine("The name of the file is not valid. The name have to be a GUID.");
                        break;
                    }

                    //Validate the file path.
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.File))
                    {
                        Console.WriteLine("The path to file is not valid.");
                    }

                    BackupEntityCommandActions.LoadContentToFile();
                    break;

                case CommandType.LoadAll:
                    //Load all files information for a specific user.

                    //Load files information + displat file information.
                    BackupEntityCommandActions.LoadAll().ToList().ForEach(WriteInformation);

                    break;

                case CommandType.Delete:
                    //Save the file to a specific path.

                    //Validate the file id.                    
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Name) ||
                        !Guid.TryParse(CommandLineArguments.Instance.Name, out fileId))
                    {
                        Console.WriteLine("The name of the file is not valid. The name have to be a GUID.");
                        break;
                    }

                    BackupEntityCommandActions.Delete();
                    break;

                case CommandType.TagSave:
                    //Save the given tag.
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Name))
                    {
                        Console.WriteLine("The tag name cannot be null or empty.");
                        break;
                    }

                    TagCommandActions.Save();

                    Console.WriteLine("The tag was saved.");
                    break;

                case CommandType.TagDelete:
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Id))
                    {
                        Console.WriteLine("The id of the tag is not specify");
                    }

                    TagCommandActions.Delete();

                    Console.WriteLine("The tag was deleted.");
                    break;

                case CommandType.TagGetParent:
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Id))
                    {
                        Console.WriteLine("The id of the tag is not specify");
                    }

                    var parentEntity = TagCommandActions.GetParent();

                    Console.WriteLine(string.Format("The parent tag: name - '{0}' id - '{1}'",
                                                        parentEntity.Name,
                                                        parentEntity.Id));
                    break;
                case CommandType.TagGetChildrens:
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Id))
                    {
                        Console.WriteLine("The id of the tag is not specify");
                    }

                    var items = TagCommandActions.GetChildrens();

                    if (items.Count == 0)
                    {
                        Console.WriteLine("The tag don't have any childrens.");
                        break;
                    }
                    StringBuilder sbItems = new StringBuilder();
                    foreach (var item in items)
                    {
                        sbItems.Append(string.Format("{0} - {1}\n\r", item.Name, item.Id));
                    }
                    Console.WriteLine(string.Format("The childrens are: {0}", sbItems));
                    break;
                case CommandType.TagLoad:
                    if (string.IsNullOrWhiteSpace(CommandLineArguments.Instance.Id))
                    {
                        Console.WriteLine("The id of the tag is not specify");
                    }

                    var tag = TagCommandActions.Load();

                    if(tag==null)
                    {
                        Console.WriteLine("There is not tag with the given id.");
                        break; 
                    }

                    Console.WriteLine(string.Format("The tag name is: {0}",tag.Name));
                    break;

                case  CommandType.TagLoadAll:
                    StringBuilder sbLoadAll = new StringBuilder();
                    foreach (var item in TagCommandActions.LoadAll())
                    {
                        sbLoadAll.Append(string.Format("{0} - {1}\n\r", item.Name, item.Id));
                    }
                    Console.WriteLine(string.Format("The childrens are: {0}", sbLoadAll));
                    break;
                case CommandType.GetBackupEntitiesByTag:
                    StringBuilder sbEntities = new StringBuilder();
                    foreach (var item in BackupEntityCommandActions.GetBackupEntitiesByTag())
                    {
                        sbEntities.Append(string.Format("{0} - {1}\n\r", item.Name, item.Id));
                    }
                    Console.WriteLine(string.Format("The entities are: {0}", sbEntities));
                    break;
                    


            }

        }

        /// <summary>
        /// Write information about the backup entity to the console.
        /// </summary>
        /// <param name="backupEntity">The backup entity.</param>
        private static void WriteInformation(BackupEntity backupEntity)
        {
            Console.WriteLine(string.Format(@"Name  -   {0}", backupEntity.Name));
            Console.WriteLine(string.Format(@"Original file name  -   {0}", backupEntity.OriginalFileName));
            Console.WriteLine(string.Format(@"Upload by  -   {0}", backupEntity.UploadBy));
            Console.WriteLine(string.Format(@"Uplaod date  -   {0}", backupEntity.UploadDate));
            Console.WriteLine(string.Format(@"Id  -   {0}", backupEntity.Id));
            Console.WriteLine(string.Format(@"Description  -   {0}", backupEntity.Description));
            Console.WriteLine(string.Format(@"Vizibility  -   {0}", backupEntity.Visibility));
            Console.WriteLine(@"---------------------------------------------------------");
        }
    }
}
